<?php

namespace app\admin\model\express;

use app\admin\model\User;
use think\Model;


class Package extends Model
{


    // 表名
    protected $name = 'express_package';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'createtime_text',
        'outtime_text',
        'signtime_text',
    ];


    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        if($value == 0){
            return "";
        }
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getOuttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['outtime']) ? $data['outtime'] : '');
        if($value == 0){
            return "";
        }
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getSigntimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['signtime']) ? $data['signtime'] : '');
        if($value == 0){
            return "";
        }
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function track()
    {
        return $this->belongsTo(Track::class, 'track_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function batch()
    {
        return $this->belongsTo(Batch::class, 'batch_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
